package com.beijiteshop.shop.ui.category.fragment

import android.content.Context
import android.util.TypedValue
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.shop.R
import com.beijiteshop.shop.base.BaseContainerFragment
import com.beijiteshop.shop.databinding.FragmentCategoryBinding
import com.beijiteshop.shop.model.api.response.CategoryBean
import com.beijiteshop.shop.ui.category.vm.CategoryVM
import com.beijiteshop.shop.ui.main.FragmentPagerAdapter
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView
import org.koin.android.viewmodel.ext.android.viewModel

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/10 19:03
 * @Description 分类
 */
class CategoryFragment private constructor() : BaseContainerFragment(R.layout.fragment_category) {

    companion object{

        fun newInstance(): Fragment{
            return CategoryFragment()
        }
    }

    private val mBinding by bindView<FragmentCategoryBinding>()
    private val mCategoryVM: CategoryVM by viewModel()

    private var mFragmentList: MutableList<Fragment> = mutableListOf()
    private var mCategoryList: MutableList<CategoryBean> = mutableListOf()

    override fun onLazyLoad() {
        requestData()
    }

    override fun initView(view: View) {
        mBinding.categoryTitleBar.titleBarTitleTv.text = resources.getString(R.string.category_slogan)
    }

    override fun initData() {

    }

    override fun initEvent() {
        mBinding.categoryTitleBar.titleBarBackIv.setOnClickListener { close() }
        startObserve()
    }

    private fun initMagicIndicator(){
        mBinding.categoryIndicator.apply {
            val commonNavigator = CommonNavigator(requireContext())
                .apply {
                    adapter = object : CommonNavigatorAdapter(){
                        override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                            val simplePagerTitleView: SimplePagerTitleView =
                                ColorTransitionPagerTitleView(context)
                            simplePagerTitleView.text = mCategoryList[index].typeName
                            simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14f)
                            simplePagerTitleView.normalColor = ContextCompat.getColor(requireContext(),R.color.text_light_dark)
                            simplePagerTitleView.selectedColor = ContextCompat.getColor(requireContext(),R.color.text_light_dark)
                            simplePagerTitleView.setOnClickListener {
                                mBinding.categoryVp.currentItem = index
                            }
                            return simplePagerTitleView
                        }

                        override fun getCount(): Int {
                            return mCategoryList.size
                        }

                        override fun getIndicator(context: Context?): IPagerIndicator {
                            val indicator = LinePagerIndicator(context)
                            indicator.setColors(ContextCompat.getColor(requireContext(),R.color.color_light_blue))
                            return indicator
                        }
                    }
                }
            this.navigator = commonNavigator
            val titleContainer = commonNavigator.titleContainer // must after setNavigator
            titleContainer.showDividers = LinearLayout.SHOW_DIVIDER_MIDDLE
            titleContainer.dividerPadding = UIUtil.dip2px(requireContext(), 15.0)
            titleContainer.dividerDrawable = ContextCompat.getDrawable(requireContext(),R.drawable.category_divider)
            ViewPagerHelper.bind(this, mBinding.categoryVp)
        }
    }

    private fun initViewPager(){
        mBinding.categoryVp.apply {
            adapter = FragmentPagerAdapter(mFragmentList,childFragmentManager)
        }
    }

    private fun requestData(){
        mCategoryVM.getCategories()
    }

    private fun startObserve(){
        mCategoryVM.apply {

            liveCategories.observeKt {
                if (!it.isNullOrEmpty()){
                    mCategoryList = it.toMutableList()
                    it.forEach {category ->
                        mFragmentList.add(CategoryListFragment.newInstance(category))
                    }
                    initViewPager()
                    initMagicIndicator()
                }
            }

            liveError.observeKt {
                showToast(it?:resources.getString(R.string.something_error))
            }

            liveNetError.observeKt {
                showToast(it?:resources.getString(R.string.state_network_error))
            }
        }
    }
}